//加载角色列表
function loadData_role() {
    $.ajax({
        url: "../role/findAll",
        dataType: "json",
        success: function (result) {
            if (result.flag) {
                //渲染数据
                renderData_role(result.data);
            } else {
                alert(result.msg);
            }
        },
        error: function (jqxhr) {
            alert("请求出错：" + jqxhr.status);
        }
    });
}
//渲染角色列表
function renderData_role(list) {
    var template = $("#role-list option:first");

    //遍历数据
    for (var i in list) {
        var option = template.clone();
        option.text(list[i].roleName);
        option.attr("value", list[i].id);

        $("#role-list").append(option);
    }
}

//校验表单
var flag = false;
function checkForm(type){
    //校验用户编码
    checkUserCode(type);
    if(!flag){
        $("#userCode")[0].setCustomValidity("该用户编码已被占用");
    }else{
        $("#userCode")[0].setCustomValidity("");
    }

    var roleList = $("#role-list")[0];
    if(roleList.value == 0){
        //提示消息，拦截表单的提交
        roleList.setCustomValidity("请选择角色");
    }else{
        //清空消息，放行
        roleList.setCustomValidity("");
    }
}
//校验用户编码
function checkUserCode(type){
    var params = {"userCode": $("#userCode").val()};
    var url;
    if(type === "add"){//添加
        url = "../user/checkCodeAdd";
    }else if(type === "upd"){//修改
        params.self = userCode;
        url = "../user/checkCodeUpd";
    }
    $.ajax({
        url: url,
        dataType: "json",
        data:params,
        async: false,
        success: function (result) {
            flag = result.flag;
        },
        error: function (jqxhr) {
            alert("请求出错：" + jqxhr.status);
        }
    });
}

//用户修改页面数据回显
function loadData_user(){
    //发送请求获取用户数据
    $.ajax({
        url: "../user/findById?id="+sessionStorage.id,
        dataType: "json",
        success: function (result) {
            if (result.flag) {
                //渲染数据
                renderData_user(result.data);
            } else {
                alert(result.msg);
            }
        },
        error: function (jqxhr) {
            alert("请求出错：" + jqxhr.status);
        }
    });
}
function renderData_user(user) {
    userCode = user.userCode;
    $("#img").attr("src",user.imgSrc)
    $("#userCode").val(user.userCode);
    $("#userName").val(user.userName);
    $("#userPassword").val(user.userPassword);
    $("#phone").val(user.phone);
    $("#address").val(user.address);
    $("#birthday").val(user.birthday);
    $("#role-list").val(user.userRole);
    if(user.gender === 1){
        $("[name='gender']:last").attr("checked", "checked");
    }else{
        $("[name='gender']:first").attr("checked", "checked");
    }
}

//加载供应商列表
function loadData_pro() {
    $.ajax({
        url: "../pro/findAll",
        dataType: "json",
        async:false,
        success: function (result) {
            if (result.flag) {
                //渲染数据
                renderData_pro(result.data);
            } else {
                alert(result.msg);
            }
        },
        error: function (jqxhr) {
            alert("请求出错：" + jqxhr.status);
        }
    });
}
//渲染供应商列表
function renderData_pro(list) {
    var template = $("#pro_list option:first");

    //遍历数据
    for (var i in list) {
        var option = template.clone();
        option.text(list[i].proName);
        option.attr("value", list[i].id);

        $("#pro_list").append(option);
    }
}


var billFlag = false;
//校验订单表单
function checkBill(type) {
    console.log("checkBill");
    //校验订单编号
    checkBillCode(type);

    if (billFlag){
        $("#billCode")[0].setCustomValidity("");
    }else {
        $("#billCode")[0].setCustomValidity("该订单编号已经被使用");
    }

    //校验供应商列表
    var proList = $("#pro_list")[0];
    console.log("proList.value: "+proList.value);
    if (proList.value == 0){
        proList.setCustomValidity("请选择供应商");
    }else {
        //放行
        proList.setCustomValidity("");
    }
}
//校验订单编号
function checkBillCode(type) {
    var params={"billCode":$("#billCode").val()};
    var url;
    if (type==='add'){
        url="../bill/checkBillCodeAdd";
    }else if (type==='upd'){
        url="../bill/checkBillCodeUpd";
        params.self=billCode;
    }
    $.ajax({
        url:url,
        dataType:"json",
        data: params,
        async: false,
        success:function (result) {
            billFlag=result.flag;
        },
        error:function (jqxhr) {
            alert("请求出错："+jqxhr.status);
        }
    });

}

//订单修改页面数据回显
function loadData_bill(){
    //发送请求获取用户数据
    $.ajax({
        url: "../bill/findById?id="+sessionStorage.id,
        dataType: "json",
        success: function (result) {
            if (result.flag) {
                //渲染数据
                renderData_bill(result.data);
            } else {
                alert(result.msg);
            }
        },
        error: function (jqxhr) {
            alert("请求出错：" + jqxhr.status);
        }
    });
}
function renderData_bill(bill){
    billCode=bill.billCode;
    $("#img").attr("src",bill.imgSrc)
    $("#billCode").val(bill.billCode);
    $("#productName").val(bill.productName);
    $("#productUnit").val(bill.productUnit);
    $("#productCount").val(bill.productCount);
    $("#totalPrice").val(bill.totalPrice);
    $("#pro_list").val(bill.providerId);
    if(bill.isPayment === 1){
        $("[name='isPayment']:first").attr("checked", "checked");
    }else{
        $("[name='isPayment']:last").attr("checked", "checked");
    }
}

//订单添加修改页面 切换图片后，发送ajax请求上传图片
function upload() {
    var img = $("#imgButton")[0].files[0];

    //FormDate对象
    var formData = new FormData();
    //添加数据，key-value
    formData.append("img", img);

    $.ajax({
        url: "../img/upload",
        type: "post",
        dataType: "json",
        data: formData,
        processData: false, //默认为true,请求参数会转化成一个查询字符串,false则不处理
        contentType: false, //指定为false才能形成正确的Content-Type="multipart/form-data"
        success: function (result) {
            if (result.flag) {
                $("#img").attr("src", "../upload/" + result.data);
            } else {
                alert(result.msg);
            }
        }
    });
}



